package us.mmapp;

import android.app.Activity;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;

import us.mmapp.util.FileNameUtil;


public class AudioActivity extends Activity
{
	private static final String LOG_TAG = "AudioRecord";

	private RecordButton mRecordButton = null;
	private MediaRecorder mRecorder = null;

	private MediaPlayer mPlayer = null;
	private String fileName;
	private void onRecord(boolean start)
	{
		if (start)
		{
			startRecording();
			//
		}
		else
		{
			stopRecording();
		}
	}

	private void startRecording()
	{
		mRecorder = new MediaRecorder();
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		fileName=FileNameUtil.getName(FileNameUtil.AUDIO);
		mRecorder.setOutputFile(fileName);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		try
		{
			mRecorder.prepare();
		}
		catch (IOException e)
		{
			Log.e(LOG_TAG, "prepare() failed");
		}
		try
		{
			mRecorder.start();
		}
		catch (IllegalStateException e)
		{
			Log.e(LOG_TAG, "start failed");
		}
	}

	private void stopRecording()
	{
		mRecorder.stop();
		mRecorder.release();
		mRecorder = null;
		Intent intent = new Intent();// intent is like the envelop
		intent.setClass(this, InfoActivity.class);// arg1 is the current class and arg2 is the class u wanna call
		Bundle bundle = new Bundle(); // bundle is like the letter
		bundle.putString("keyword", fileName);// arg1 is the keyword of the txt, arg2 is the txt
		bundle.putBoolean("new", true);
		intent.putExtras(bundle);// actually it's bundle who carries the content u wanna pass
		startActivityForResult(intent, 0);
		finish();
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		startActivity(new Intent(this, HomeScreenActivity.class));
	}
	class RecordButton extends Button
	{
		boolean mStartRecording = true;

		OnClickListener clicker = new OnClickListener()
		{
			public void onClick(View v)
			{
				if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
				{
					onRecord(mStartRecording);
					if (mStartRecording)
					{
						setText("Stop recording");
					}
					else
					{
						setText("Start recording");
					}
					mStartRecording = !mStartRecording;
				}
				else
				{
					Toast.makeText(getApplicationContext(), "External Storage Error", Toast.LENGTH_SHORT).show();
				}

			}
		};

		public RecordButton(Context ctx)
		{
			super(ctx);
			setText("Start recording");
			setOnClickListener(clicker);
		}
	}

	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		RelativeLayout rl = new RelativeLayout(this);
		mRecordButton = new RecordButton(this);
		mRecordButton.setText("Start recording");
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(142, RelativeLayout.LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(mRecordButton, lp);
		setContentView(rl);
	}

	@Override
	public void onPause()
	{
		super.onPause();
		if (mRecorder != null)
		{
			mRecorder.release();
			mRecorder = null;
		}

		if (mPlayer != null)
		{
			mPlayer.release();
			mPlayer = null;
		}
	}
}